﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ToDoList.Logic
{
    public class DatailedListSource
    {
        /// <summary>
        /// 创建清单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool CreateList(int userId, string listName)
        {
            var user = TestData.Users.SingleOrDefault(Id => Id.UserId == userId);
            var number = user.DetailedLists.Max(n => n.ListId) + 1;
            user.DetailedLists = new List<DetailedList>();
            if (user == null)
            {
                return false;
            }
            else
            {
                var createList = new DetailedList
                {
                    UserId = userId,
                    ListName = listName,
                    ListId = number,
                    CreationTime = DateTime.Now,
                };
                TestData.Users.SingleOrDefault(Id => Id.UserId == userId).DetailedLists.Add(createList);
                TestData.DetailedLists.Add(createList);
                return true;
            }

        }
        /// <summary>
        /// 浏览清单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<DetailedList> BrowseList(int userId)
        {
            var user = TestData.Users.SingleOrDefault(Id => Id.UserId == userId);
            if (user.DetailedLists != null)
            {
                return user.DetailedLists.ToList();
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 添加事件
        /// </summary>
        /// <param name="lsitId"></param>
        /// <param name="OverdueTime"></param>
        /// <param name="ItemsContent"></param>
        /// <returns></returns>
        public bool AddItems(int userId, int listId, string OverdueTime, string ItemsContent)
        {
            var inspect = TestData.Users.SingleOrDefault(Id => Id.UserId == userId)
                .DetailedLists.SingleOrDefault(d => d.ListId == listId);
            var number = inspect.WaitToDoLists.Max(n => n.EventID) + 1;
            if (inspect == null)
            {
                return false;
            }
            else
            {
                var addWaitToDoList = new WaitToDoList()
                {
                    CreationUser = userId,
                    CreationTime = DateTime.Now,
                    OverdueEvent = DateTime.Parse(OverdueTime),
                    State = false,
                    Content = ItemsContent,
                    EventID = number
                };
                inspect.WaitToDoLists.Add(addWaitToDoList);
                TestData.WaitToDoLists.Add(addWaitToDoList);
                return true;
            }
        }
        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="listId"></param>
        /// <param name="itemsId"></param>
        /// <returns></returns>
        public bool DeleteItems(int userId, int listId, int itemsId)
        {
            var list = TestData.DetailedLists.SingleOrDefault(Id => Id.ListId == listId);
            var matter = TestData.WaitToDoLists.SingleOrDefault(Id => Id.EventID == itemsId);
            if (list == null && matter == null)
            {
                return false;
            }
            else
            {
                list.WaitToDoLists.Remove(matter);
                TestData.WaitToDoLists.Remove(matter);
                TestData.Users.SingleOrDefault(name => name.UserId == userId)
                    .WaitToDoLists.Remove(matter);
                return true;
            }
        }
        /// <summary>
        /// 修改清单名字
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="listId"></param>
        /// <param name="newListName"></param>
        /// <returns></returns>
        public bool ModifyListName(int userId, int listId, string newListName)
        {
            var list = TestData.DetailedLists.SingleOrDefault(Id => Id.ListId == listId);
            var userName = TestData.Users.SingleOrDefault(name => name.UserId == userId);
            if(list == null && userName == null)
            {
                return false;
            }
            else
            {
                list.ListName = newListName;
                return true;
            }
        }
    }
}
